package nl.hu.verbrandendephoenix.controller;

import java.util.ArrayList;

import nl.hu.verbrandendephoenix.client.AppManager;
import nl.hu.verbrandendephoenix.client.NotificationManager;
import nl.hu.verbrandendephoenix.shared.FieldVerifier;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class SearchUser {
	private nl.hu.verbrandendephoenix.view.SearchUser searchUserForm;

	public SearchUser() { }

	public Panel getPanel() {
		searchUserForm = new nl.hu.verbrandendephoenix.view.SearchUser();
		Panel p = searchUserForm.constructForm();
		addHandlers();
		return p;
	}

	private void processSubmit() {
		NotificationManager nM =  NotificationManager.get();

		if(!checkFields()) {
			nM.fieldCheck();
		} else {
			String strSearchInput = searchUserForm.getTxtSearch().getText();
			
			AppManager.get().getRpcService().searchUser(strSearchInput, new AsyncCallback<ArrayList<String>>() {
				public void onFailure(Throwable caught) {
					Window.alert("Communication Failure!");
				}
				public void onSuccess(ArrayList<String> result) {
					setResultPanel(result);
				}
			});
		}
	}

	private void setResultPanel(ArrayList<String> arrUsers) {
		VerticalPanel pnlResult = searchUserForm.getPnlResult();
		pnlResult.clear();
		if(arrUsers.size() > 0) {
			Grid grdResult = new Grid(arrUsers.size(), 3);
			int i = 0;
			for(String u : arrUsers) {
				grdResult.setWidget(i, 0, new Label(u));
				grdResult.setWidget(i, 1, new Anchor("Wijzig Gegevens", "#editprofile-" + u));
				grdResult.setWidget(i, 2, new Anchor("Blokkeer Account", "#blockuser-" + u));
				i++;
			}
			pnlResult.add(grdResult);
		} else {
			pnlResult.add(new Label("Er is geen gebruiker gevonden."));
		}
	}
	
	private boolean checkFields() {
		FieldVerifier fv = FieldVerifier.get();
		boolean b = true; 

		TextBox txtSearch = searchUserForm.getTxtSearch();
		if(!fv.checkString(3, 30, txtSearch.getText())) {
			fv.addRequired(txtSearch);
			b = false;
		} else {
			fv.removeRequired(txtSearch);
		}
		return b;
	}

	public void addHandlers() {
		Button btnSearch = searchUserForm.getBtnSearch();
		btnSearch.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				processSubmit();
			} 
		});

		TextBox tb = searchUserForm.getTxtSearch();
		tb.addKeyPressHandler(new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if(event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
					processSubmit();
				}
			}
		});
	}
}
